package com.asiainfo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.asiainfo.bean.UserBean;
import com.asiainfo.bean.UserSessionBean;
import com.asiainfo.dao.BaseDao;
import com.asiainfo.util.SessionInfoUtil;

@Controller
@RequestMapping("/userController")
public class UserController extends ModelAndView{
	
	private Logger logger = LogManager.getLogger(this.getClass());
	
	
	/**
	 * 数据操作封装DAO
	 */
	@Autowired
	private BaseDao dao;

	/**
	 * 方法描述：跳转到我的信息页面
	 * 
	 * return 我的信息页面Model
	 */
	@RequestMapping("/myInfoView.do")
	public ModelAndView myInfoView() {
		this.setViewName("user/myInfo");
		
		return this;	
		}

	
	
	/**
	 * 方法描述：显示我的信息操作
	 * 
	 */
	@RequestMapping(value = "/myInfo.do")
	 
	public @ResponseBody
	JSONObject myInfo(@RequestBody UserBean userBean, 
			HttpServletRequest request) throws Exception {
		
         JSONObject retMsg = new JSONObject();
		
		UserSessionBean userSessionBean = SessionInfoUtil.getUserSessBean(request.getSession());
		
		String userPhone = userSessionBean.getPhone();
		
		if(userPhone !=null){
			UserBean userInfo = dao.getDAO().selectOne(
					"UserMapper.queryUserInfo", userPhone);
			
			this.addObject("userInfo", userInfo);
			retMsg.put("flag", true);
		}else{
			retMsg.put("flag", false);
		}
		return retMsg;
	}
	

	/**
	 * 方法描述：更新我的信息操作
	 * 
	 */
	@RequestMapping(value = "/updateMyInfo.do")
	 
	public @ResponseBody
	JSONObject updateMyInfo(@RequestBody UserBean userBean, 
			HttpServletRequest request) throws Exception {
		
         JSONObject retMsg = new JSONObject();
		
		UserSessionBean userSessionBean = SessionInfoUtil.getUserSessBean(request.getSession());
		
		String userPhone = userSessionBean.getPhone();
		
		if(userPhone !=null){
			 dao.getDAO().update(
					"UserMapper.updateUserInfo", userBean);
			
			
			retMsg.put("flag", true);
		}else{
			retMsg.put("flag", false);
		}
		return retMsg;
	}
	
}
